<?php
/**
 * BaseUtil
 *
 * @author fusq
 */
class BaseUtil {
    public static function getModulePath($key){
        $modules = Zend_Registry::get ( 'modules');
        return $modules[$key];
    }

    public static function getLibraryPath(){
        if(file_exists("../conf/config.xml")){
            $xml = simplexml_load_file('../conf/config.xml');
            return $xml->production->libraries->library;
        }else{
            echo "Config file doesn't exist.";
            exit();
        }
    }

    public static function getInstallConfig(){
        if(file_exists("../conf/install.xml")){
            $configs = array();
            $xml = simplexml_load_file('../conf/install.xml');
            $configs['php'] =  $xml->env->version->php;
            $configs['mysql'] =  $xml->env->version->mysql;
            $configs['apache'] =  $xml->env->version->apache;
            $configs['dbaction'] = $xml->action->database;
            return $configs;
        }else{
            echo "Install config file doesn't exist.";
            exit();
        }
    }

    public static function getLoginUser(){
        BaseUtil::isLogined();
        return $_SESSION['loginUser'];
    }

    public static function isLogined(){
         $baseUrl = HttpUtil::baseUrl();
         if(isset($_SESSION['loginUser']) && !empty($_SESSION['loginUser'])){
               return true;
         }else{
               header("Location: " . $baseUrl . "/acl/admin/login");
               exit();
         }
    }

    public static function clearSession(){
        $_SESSION = array();
        session_destroy();
    }
    
    public static function uploadFile($dirname,$name=null,$fileType=array(),$size=0){
    	$dirname = str_replace('\\','/',$dirname);
    	if(!is_dir($dirname.'/')){
    		mkdir($dirname,0777,true);
    	}
    	$adapter = new Zend_File_Transfer_Adapter_Http();
    	$adapter->setDestination($dirname.'/');
    	if(!empty($fileType)){
    	    $adapter->addValidator('Extension',false,$fileType);
    	}
    	if($size > 0){
            $adapter->addValidator('Size', false, $size);
    	}
    	$oldname = $adapter->getFileName($name);
    	
    	if(empty($oldname)){
    		$result['file_path'] = "";
    		return $result;
    	}
    	
		$oldname = str_replace('\\','/',$oldname);
		
		$path_parts = pathinfo(strval($oldname));
		
		$extension = $path_parts['extension'];
		$filename = $path_parts['filename'];
        if($extension) {
			$created = date ( 'Y-m-d H:i:s' );
			$newFileName = $filename.'_'.strtotime($created).'.'.$extension;
		}
		$newFilePath = $dirname.'/'.$newFileName;
		$adapter->addFilter('Rename',$newFilePath);
		$result = array();
        if (! $adapter->receive ($name)) {
        	//$fileInfo = $adapter->getFileInfo($name);
			$messages = $adapter->getMessages ();
            $error = implode ( "\n", $messages );
			
            $result['error'] = $error;
		} else {
			$result['file_path'] = $newFileName;
		}
		
		return $result;
    }
    
    public static function paginate($curPage,$totalNum){
    	$numPerPage = "20";
    	$pageNum = ceil($totalNum/$numPerPage);
    	$offSet = ($curPage-1)*$numPerPage;
    	$numOfPage = $curPage<$pageNum?$numPerPage:$totalNum-$numPerPage*($pageNum-1);
    	$result = array(
    	    "cur_page" => $curPage,
    	    "num_of_page" => $numOfPage,
    	    "page_num" => $pageNum,
    	    "offset" => $offSet
    	);
    	
    	return $result;
    	
    }
}

